package com.dlwx.parttimetalent.views;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.bean.OrderRecBean;
import com.dlwx.parttimetalent.inter.DialogFilerInter;
import com.dlwx.parttimetalent.utils.ScreenUtils;
import com.dlwx.parttimetalent.utils.SizeUtils;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.flyco.dialog.widget.base.BaseDialog;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/23.
 */

public class ClassifyDialog extends BaseDialog {
    private final double itemWidth;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.flowlayout)
    TagFlowLayout flowlayout;
    private Context context;
    private DialogFilerInter inter;
    List<OrderRecBean.BodyBean.ChildBeanX> beans;

    public ClassifyDialog(Context context, List<OrderRecBean.BodyBean.ChildBeanX> beans,DialogFilerInter inter) {
        super(context);
        this.context = context;
        this.beans = beans;
        this.inter=inter;
        double width= ScreenUtils.getScreenWidth()*0.92;
        itemWidth=(int) (width - SizeUtils.dp2px(40f)) / 5;;
    }

    @Override
    public View onCreateView() {
        widthScale(0.92f);
//        showAnim(new Swing());
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_classify, null);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        flowlayout.setMaxSelectCount(1);
        final TagAdapter<OrderRecBean.BodyBean.ChildBeanX> tagAdapter1 = new TagAdapter<OrderRecBean.BodyBean.ChildBeanX>(beans) {
            @Override
            public View getView(com.zhy.view.flowlayout.FlowLayout parent, int position, OrderRecBean.BodyBean.ChildBeanX bean) {
                TextView textView = (TextView) LayoutInflater.from(context).inflate(R.layout.tv_label_red_select
                        , flowlayout, false);
                textView.setTextSize(12);
                textView.setText(bean.getChildname());
                textView.getLayoutParams().width = (int)itemWidth;
                return textView;
            }
        };
        flowlayout.setAdapter(tagAdapter1);
    }

    @Override
    public void setUiBeforShow() {

    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                this.dismiss();
                break;
            case R.id.tv_confirm:
                Set<Integer> sets = flowlayout.getSelectedList();
                if(sets.isEmpty()){
                    ToastUtils.toastForShort(context,"请选择一个标签");return;
                }
                int position=sets.iterator().next();

                inter.callBack( beans.get(position).getLabelid()+"","","","","","","");
                this.dismiss();
                break;
        }
    }
}
